package de.softwareforge.published.util;

/*
 * Copyright (C) 2007-2008 Henning P. Schmiedehausen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.MessageFormat;
import java.util.ResourceBundle;

import com.sun.javadoc.DocErrorReporter;
import com.sun.javadoc.SourcePosition;


/**
 * Locale management object for displaying messages/warnings/errors.
 *
 * @author <a href="mailto:henning@schmiedehausen.org">Henning P. Schmiedehausen</a>
 * @version $Id:Locale.java 357 2007-08-13 20:57:20Z hps@intermeta.de $
 * @api.level unpublished
 */
public class Locale
{
    private final String bundleName;

    private final DocErrorReporter errorReporter;

    private ResourceBundle resourceBundle = null;

    /**
     * Creates a new Locale object.
     *
     * @param bundleName The name of the bundle to load.
     * @param errorReporter An {@link DocErrorReporter} object to send messages out.
     */
    public Locale(final String bundleName, final DocErrorReporter errorReporter)
    {
        this.errorReporter = errorReporter;
        this.bundleName = bundleName;
    }

    /**
     * Lookup a key in the locale bundle.
     *
     * @param msg The message key to look up.
     *
     * @return A localized string for the given message.
     */
    public String getText(final LocaleMsg msg)
    {
        return getText(msg, null, null, null);
    }

    /**
     * Lookup a key in the locale bundle.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     *
     * @return A localized string for the given message.
     */
    public String getText(final LocaleMsg msg, final String arg1)
    {
        return getText(msg, arg1, null, null);
    }

    /**
     * Lookup a key in the locale bundle.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     *
     * @return A localized string for the given message.
     */
    public String getText(final LocaleMsg msg, final String arg1, final String arg2)
    {
        return getText(msg, arg1, arg2, null);
    }

    /**
     * Lookup a key in the locale bundle.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     * @param arg3 Argument for the third placeholder in the localized string.
     *
     * @return A localized string for the given message.
     */
    public String getText(final LocaleMsg msg, final String arg1, final String arg2, final String arg3)
    {

        if (resourceBundle == null)
        {
            resourceBundle = ResourceBundle.getBundle(bundleName);
        }

        return MessageFormat.format(resourceBundle.getString(msg.toString()), arg1, arg2, arg3);
    }

    /**
     * Output a localized error message.
     *
     * @param msg The message key to look up.
     */
    public void error(final LocaleMsg msg)
    {
        errorReporter.printError(getText(msg));
    }

    /**
     * Output a localized error message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     */
    public void error(final SourcePosition pos, final LocaleMsg msg, final String arg1)
    {
        errorReporter.printError(pos, getText(msg, arg1));
    }

    /**
     * Output a localized error message.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     */
    public void error(final LocaleMsg msg, final String arg1)
    {
        errorReporter.printError(getText(msg, arg1));
    }

    /**
     * Output a localized error message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     */
    public void error(final SourcePosition pos, final LocaleMsg msg, final String arg1, final String arg2)
    {
        errorReporter.printError(pos, getText(msg, arg1, arg2));
    }

    /**
     * Output a localized error message.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     */
    public void error(final LocaleMsg msg, final String arg1, final String arg2)
    {
        errorReporter.printError(getText(msg, arg1, arg2));
    }

    /**
     * Output a localized error message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     * @param arg3 Argument for the third placeholder in the localized string.
     */
    public void error(final SourcePosition pos, final LocaleMsg msg, final String arg1, final String arg2, final String arg3)
    {
        errorReporter.printError(pos, getText(msg, arg1, arg2, arg3));
    }

    /**
     * Output a localized error message.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     * @param arg3 Argument for the third placeholder in the localized string.
     */
    public void error(final LocaleMsg msg, final String arg1, final String arg2, final String arg3)
    {
        errorReporter.printError(getText(msg, arg1, arg2, arg3));
    }

    /**
     * Output a localized warning message.
     *
     * @param msg The message key to look up.
     */
    public void warn(final LocaleMsg msg)
    {
        errorReporter.printWarning(getText(msg));
    }

    /**
     * Output a localized warning message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     */
    public void warn(final SourcePosition pos, final LocaleMsg msg)
    {
        errorReporter.printWarning(pos, getText(msg));
    }

    /**
     * Output a localized warning message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     */
    public void warn(final SourcePosition pos, final LocaleMsg msg, final String arg1)
    {
        errorReporter.printWarning(pos, getText(msg, arg1));
    }

    /**
     * Output a localized warning message.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     */
    public void warn(final LocaleMsg msg, final String arg1)
    {
        errorReporter.printWarning(getText(msg, arg1));
    }

    /**
     * Output a localized warning message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     */
    public void warn(final SourcePosition pos, final LocaleMsg msg, final String arg1, final String arg2)
    {
        errorReporter.printWarning(pos, getText(msg, arg1, arg2));
    }

    /**
     * Output a localized warning message.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     */
    public void warn(final LocaleMsg msg, final String arg1, final String arg2)
    {
        errorReporter.printWarning(getText(msg, arg1, arg2));
    }

    /**
     * Output a localized warning message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     * @param arg3 Argument for the third placeholder in the localized string.
     */
    public void warn(final SourcePosition pos, final LocaleMsg msg, final String arg1, final String arg2, final String arg3)
    {
        errorReporter.printWarning(pos, getText(msg, arg1, arg2, arg3));
    }

    /**
     * Output a localized warning message.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     * @param arg3 Argument for the third placeholder in the localized string.
     */
    public void warn(final LocaleMsg msg, final String arg1, final String arg2, final String arg3)
    {
        errorReporter.printWarning(getText(msg, arg1, arg2, arg3));
    }

    /**
     * Output a localized message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     */
    public void notice(final SourcePosition pos, final LocaleMsg msg)
    {
        errorReporter.printNotice(pos, getText(msg));
    }

    /**
     * Output a localized message.
     *
     * @param msg The message key to look up.
     */
    public void notice(final LocaleMsg msg)
    {
        errorReporter.printNotice(getText(msg));
    }

    /**
     * Output a localized message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     */
    public void notice(final SourcePosition pos, final LocaleMsg msg, final String arg1)
    {
        errorReporter.printNotice(pos, getText(msg, arg1));
    }

    /**
     * Output a localized message.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     */
    public void notice(final LocaleMsg msg, final String arg1)
    {
        errorReporter.printNotice(getText(msg, arg1));
    }

    /**
     * Output a localized message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     */
    public void notice(final SourcePosition pos, final LocaleMsg msg, final String arg1, final String arg2)
    {
        errorReporter.printNotice(pos, getText(msg, arg1, arg2));
    }

    /**
     * Output a localized message.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     */
    public void notice(final LocaleMsg msg, final String arg1, final String arg2)
    {
        errorReporter.printNotice(getText(msg, arg1, arg2));
    }

    /**
     * Output a localized message.
     *
     * @param pos The position in the source code.
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     * @param arg3 Argument for the third placeholder in the localized string.
     */
    public void notice(final SourcePosition pos, final LocaleMsg msg, final String arg1, final String arg2, final String arg3)
    {
        errorReporter.printNotice(pos, getText(msg, arg1, arg2, arg3));
    }

    /**
     * Output a localized message.
     *
     * @param msg The message key to look up.
     * @param arg1 Argument for the first placeholder in the localized string.
     * @param arg2 Argument for the second placeholder in the localized string.
     * @param arg3 Argument for the third placeholder in the localized string.
     */
    public void notice(final LocaleMsg msg, final String arg1, final String arg2, final String arg3)
    {
        errorReporter.printNotice(getText(msg, arg1, arg2, arg3));
    }

    /**
     * Output an error message. Only for debugging!
     *
     * @param msg The message to report.
     */
    public void printError(final String msg)
    {
        errorReporter.printError(msg);
    }

    /**
     * Output an warn message. Only for debugging!
     *
     * @param msg The message to report.
     */
    public void printWarning(final String msg)
    {
        errorReporter.printWarning(msg);
    }

    /**
     * Output an notice message. Only for debugging!
     *
     * @param msg The message to report.
     */
    public void printNotice(final String msg)
    {
        errorReporter.printNotice(msg);
    }

}
